package guestbook;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import datebase.Update;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Select extends JInternalFrame {
	private JLabel lblNewLabel;
	private JTextField findstr;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;

	Connection con;
	GuestBookMain gbm;
	DefaultTableModel model;

	/**
	 * Create the frame.
	 */
	public Select(Connection c, GuestBookMain o) {
		super("select", true, true, true, true);
		this.con = c;
		this.gbm = o;

		setTitle("방명록 조회");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getFindstr());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());

		setData();
	}

	public void setData() {
		String sql = "select * from guestbook where mid = ? or subJect like ? or content like ? ";
		String find = findstr.getText();

		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, find);
			pstmt.setString(2, "%" + find + "%");
			pstmt.setString(3, "%" + find + "%");

			ResultSet rs = pstmt.executeQuery();

			// 컬럼명 테이블에 셋팅
			ResultSetMetaData rsm = rs.getMetaData();
			Vector col = new Vector<>();
			for (int i = 1; i <= rsm.getColumnCount(); i++) {
				col.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(col, 0);
			table.setModel(model);

			// 데이터 추가
			while (rs.next()) {
				Vector d = new Vector<>();
				for (int i = 1; i <= rsm.getColumnCount(); i++) {
					d.add(rs.getString(i));
				}
				model.addRow(d);
			}
		} catch (Exception ex) {

		}
	}

	// public void MouseC() {
	// int index = table.getSelectedRow();
	// Vector<String> vec = new Vector<String>();
	// vec.add((String) table.getValueAt(index, 0));
	// vec.add((String) table.getValueAt(index, 1));
	// vec.add((String) table.getValueAt(index, 2));
	//
	// if (gbm.update == null) {
	// gbm.update = new Update(con, gbm);
	// gbm.contentPane.add(gbm.update);
	// gbm.update.setVisible(true);
	// gbm.update.toFront();
	// }
	// }

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("찾는자료 : ");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(12, 20, 76, 15);
		}
		return lblNewLabel;
	}

	private JTextField getFindstr() {
		if (findstr == null) {
			findstr = new JTextField();
			findstr.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent ev) {
					if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
						setData();
					}
				}
			});
			findstr.setBounds(87, 17, 226, 21);
			findstr.setColumns(10);
		}
		return findstr;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

				}
			});
			btnNewButton.setBounds(325, 16, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 55, 410, 206);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					if (gbm.update == null) {
						gbm.openUpdate();
					}
					gbm.update.setVisible(true);
					gbm.update.toFront();

					int row = table.getSelectedRow();
					int serial = Integer.parseInt((String) table.getValueAt(
							row, 0)); // 시리얼 행

					gbm.update.select(serial);

					// MouseC();
				}
			});
		}
		return table;
	}
}
